//=============================================================
// 创建人:            ssd
// 创建时间:          8/17/2024 2:42:40 PM
// 邮箱：             1292934053@qq.com
//==============================================================
using System.Collections.Generic;
using System.Linq.Expressions;
using LF.Common;
using LF.Domain.Entities;
using LF.Domain.Helper;

namespace LF.Domain.Services.Iml
{
    public class ChinaManagerService : IChinaManagerService
    {
        public List<China> All(int index, int pageSize,string? key)
        {
            var query = FreeSqlHelper.DbContext.Queryable<China>();
            if (!key.IsNullOrWhiteSpace())
            {
                //query = query.Where(p => p.XX.Contains(key) || p.XX.Contains(key));
            }
            var list = query
                .OrderByDescending(p => p.UpdateTime)
                .Page(index, pageSize)
                .ToList();
            return list;
        }

        public List<China> List(Expression<Func<China, bool>>? where = null)
        {
            var query = FreeSqlHelper.DbContext.Queryable<China>();
            if (where != null)
            {
                query = query.Where(where);
            }
            var list = query
                .OrderByDescending(p => p.UpdateTime)
                .ToList();
            return list;
        }

        public List<China> Area(string province, string city)
        {
            var list = FreeSqlHelper.DbContext.Queryable<China>()
                .Where(p => p.Province == province && p.City == city && p.Area != "0" && p.Town == "0")
                .OrderBy(p => p.Code)
                .ToList();
            return list;
        }

        public List<China> City(string province)
        {
            Expression<Func<China, bool>> where = p => p.Province == province && p.City != "0" && p.Area == "0" && p.Town == "0";
            List<China> list;
            var query = FreeSqlHelper.DbContext.Queryable<China>().Where(where);
            if (query.Any())
            {
                list = FreeSqlHelper.DbContext.Queryable<China>().Where(where)
                    .ToList();
            }
            else
            {
                list = FreeSqlHelper.DbContext.Queryable<China>().Where(p => p.Province == province && p.City != "0" && p.Town == "0")
                    .ToList();
            }
            return list;
        }

        public long Count(string? key)
        {
            var query = FreeSqlHelper.DbContext.Queryable<China>();
            if (!key.IsNullOrWhiteSpace())
            {
                //query = query.Where(p => p.XX.Contains(key) || p.XX.Contains(key));
            }
            var count = query.Count();
            return count;
        }

        public China Get(int id)
        {
            var item = FreeSqlHelper.DbContext.Queryable<China>().Where(p => p.Id == id).First();
            return item;
        }

        public string GetInfo(int id, AddrType type)
        {
            var china = Get(id);
            if (china == null)
                return string.Empty;
            var province = FreeSqlHelper.DbContext.Queryable<China>().Where(p => p.Province == china.Province && p.City == "0" && p.Area == "0" && p.Town == "0").First().Name;
            Expression<Func<China, bool>> where = p => p.Province == china.Province && p.City != "0" && p.Area == "0" && p.Town == "0";
            var query = FreeSqlHelper.DbContext.Queryable<China>().Where(where);
            if (query.Any())
            {
                var city = FreeSqlHelper.DbContext.Queryable<China>().Where(p=>p.Province == china.Province && p.City == china.City && p.Area == "0" && p.Town == "0").First().Name;
                return type == AddrType.City ? $"{province}/{city}" : $"{province}/{city}/{china.Name}";

            }
            else
            {
                return $"{province}/{china.Name}";
            }

        }

        public bool Insert(China china)
        {
            var result = FreeSqlHelper.DbContext.Insert(china).ExecuteAffrows();
            return result > 0;
        }
        public China GetProvinceByCode(string code)
        {
            var model = FreeSqlHelper.DbContext.Queryable<China>()
             .Where(p => p.Province == code && p.City == "0" && p.Area == "0" && p.Town == "0")
             .OrderBy(p => p.Code)
             .First();
            return model;
        }
        public China GetCityByCode(string provincecode,string citycode)
        {
            var model = FreeSqlHelper.DbContext.Queryable<China>()
             .Where(p => p.City == citycode && p.Province == provincecode && p.Area == "0" && p.Town == "0")
             .OrderBy(p => p.Code)
             .First();
            return model;
        }
        public China GetDistrictByCode(string provincecode, string citycode,string districtcode)
        {
            var model = FreeSqlHelper.DbContext.Queryable<China>()
             .Where(p => p.City == citycode && p.Province == provincecode && p.Area == districtcode && p.Town == "0")
             .OrderBy(p => p.Code)
             .First();
            return model;
        }
        public Common.ChinaDistrict GetAddressInfo(int id)
        {
            var area=FreeSqlHelper.DbContext.Queryable<China>()
             .Where(p => p.Id==id)
             .OrderBy(p => p.Code)
             .First();
            var province= FreeSqlHelper.DbContext.Queryable<China>()
             .Where(p => p.Province == area.Province && p.City == "0" && p.Area == "0" && p.Town == "0")
             .OrderBy(p => p.Code)
             .First();
            var city = FreeSqlHelper.DbContext.Queryable<China>()
             .Where(p => p.Province == area.Province && p.City == area.City && p.Area == "0" && p.Town == "0")
             .OrderBy(p => p.Code)
             .First();
             var district = FreeSqlHelper.DbContext.Queryable<China>()
            .Where(p => p.Province == area.Province && p.City == area.City && p.Area == area.Area && p.Town == "0")
            .OrderBy(p => p.Code)
            .First();

            return new Common.ChinaDistrict { Province = province?.Name?? city?.Name, City = city?.Name?? province?.Name, District = district?.Name };


        }
        public List<China> Province()
        {
            var list = FreeSqlHelper.DbContext.Queryable<China>()
             .Where(p => p.Province != "0" && p.City == "0" && p.Area == "0" && p.Town == "0")
             .OrderBy(p => p.Code)
             .ToList();
            return list;
        }

        public List<China> Town(string province, string city, string area)
        {
            var list = FreeSqlHelper.DbContext.Queryable<China>()
            .Where(p => p.Province == province && p.City == city && p.Area == area && p.Town != "0")
            .OrderBy(p => p.Code)
            .ToList();
            return list;
        }

        public bool Update(China china)
        {
            var result = FreeSqlHelper.DbContext.Update<China>(china.Id)
               .SetSource(china)
               .ExecuteAffrows();
            return result > 0;
        }
    }
}

